/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ebdesk.view.dhkp;
import com.ebdesk.view.MainFrame;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author mbenk
 */
public class PrintDialog extends javax.swing.JDialog {
    
    /**
     * A return status code - returned if Cancel button has been pressed
     */
    public static final int RET_CANCEL = 0;
    /**
     * A return status code - returned if OK button has been pressed
     */
    public static final int RET_OK = 1;
   
    
//    private UiDHKP parent;
    private MainFrame parent;
    /**
     * Creates new form PrintDialog
     */
    public PrintDialog(MainFrame parent, boolean modal) throws Exception{
        
        super(parent, modal);
        this.parent = parent;
        initComponents();

        // Close the dialog when Esc is pressed
        String cancelName = "cancel";
        InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
        ActionMap actionMap = getRootPane().getActionMap();
        actionMap.put(cancelName, new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                doClose(RET_CANCEL);
            }
        });
    }
    
    /**
     * @return the return status of this dialog - one of RET_OK or RET_CANCEL
     */
    public int getReturnStatus() {
        return returnStatus;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ButtonGroup1 = new javax.swing.ButtonGroup();
        ButtonGroup2 = new javax.swing.ButtonGroup();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        RBPrintAll = new javax.swing.JRadioButton();
        RBPOP = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        RBPCP = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        RBPrintPart = new javax.swing.JRadioButton();
        TFPOP = new javax.swing.JTextField();
        TFFrom = new javax.swing.JTextField();
        TFTo = new javax.swing.JTextField();

        setTitle("Print Option");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImage(null);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        ButtonGroup1.add(RBPrintAll);
        RBPrintAll.setSelected(true);
        RBPrintAll.setText("Print Semua");
        RBPrintAll.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                RBPrintAllMouseClicked(evt);
            }
        });

        ButtonGroup2.add(RBPOP);
        RBPOP.setText("Print One Page");
        RBPOP.setEnabled(false);
        RBPOP.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                RBPOPMouseClicked(evt);
            }
        });

        jLabel1.setText("Page");

        ButtonGroup2.add(RBPCP);
        RBPCP.setText("Print Custom Page");
        RBPCP.setEnabled(false);
        RBPCP.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                RBPCPMouseClicked(evt);
            }
        });

        jLabel2.setText("Page");

        jLabel3.setText("to");

        ButtonGroup1.add(RBPrintPart);
        RBPrintPart.setText("Print Sebagian");
        RBPrintPart.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                RBPrintPartMouseClicked(evt);
            }
        });

        TFPOP.setEnabled(false);
        TFPOP.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TFPOPKeyPressed(evt);
            }
        });

        TFFrom.setEnabled(false);
        TFFrom.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TFFromKeyPressed(evt);
            }
        });

        TFTo.setEnabled(false);
        TFTo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TFToKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(RBPrintAll)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cancelButton))
                            .addComponent(RBPrintPart)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(RBPCP)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel2))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(RBPOP)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TFPOP, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(TFFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(TFTo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(57, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(RBPrintAll)
                .addGap(18, 18, 18)
                .addComponent(RBPrintPart)
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RBPOP)
                    .addComponent(jLabel1)
                    .addComponent(TFPOP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RBPCP)
                    .addComponent(jLabel2)
                    .addComponent(TFFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(TFTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        getRootPane().setDefaultButton(okButton);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    //Setter dan Getter Komponen Swing
   
    
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        int confirm;
        String halaman;
        String halamanAwal,halamanAkhir;
        String camat,lurah;
        String tahun;
        int jumHal;
        
        try{
            jumHal = (parent.noHal() - 1) + 2;      
            camat = parent.getKecamatan();
            lurah = parent.getKelurahan();
            tahun = parent.getTahun();
            //Untuk print semua halaman
            if(RBPrintAll.isSelected() == true){
                confirm = JOptionPane.showOptionDialog(parent, "Apakah anda yakin akan print data:\n"
                                                        +"Kecamatan : "+camat.substring(6) +"\n"
                                                        +"Kelurahan : "+lurah.substring(6) +"\n"
                                                        +"Tahun : "+tahun+"\n"
                                                        +"Halaman :Semua halaman","Confirmation", 
                                                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, 
                                                        null, null, null);
                if(confirm == JOptionPane.OK_OPTION){
                    parent.PrintAllPage();
                   
                }
            }
            
            //Untuk print satu halaman 
            if(RBPOP.isSelected() == true){
                halaman = getOnePage();
                //print tidak boleh kosong
                if(halaman.isEmpty()){
                    JOptionPane.showMessageDialog(parent, "Halaman masih kosong","Warning",JOptionPane.WARNING_MESSAGE);
                }
                //print tidak boleh di luar rentang
                else if(Integer.parseInt(halaman) < 1 ||Integer.parseInt(halaman) > jumHal){
                    JOptionPane.showMessageDialog(parent, "Masukkan berada di luar rentang halaman","Error",JOptionPane.ERROR_MESSAGE);
                }
                else{
                    confirm = JOptionPane.showOptionDialog(parent, "Apakah anda yakin akan print data:\n"
                                                            +"Kecamatan : "+camat+"\n"
                                                            +"Kelurahan : "+lurah+"\n"
                                                            +"Tahun : "+tahun+"\n"
                                                            +"Halaman : "+halaman,"Confirmation", 
                                                            JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, 
                                                            null, null, null);
                    if(confirm == JOptionPane.OK_OPTION){
                        if(Integer.parseInt(halaman) == 1){
                            parent.PrintCover();
                        }
                        else if(Integer.parseInt(halaman) == jumHal){
                            parent.PrintKetetapan();
                        }
                        else{
                            parent.PrintOnePage();
                        }
                    }     
                }
            }
            //Untuk print custom halaman
            if(RBPCP.isSelected() == true){
                halamanAwal = getPageFrom();
                halamanAkhir = getPageTo();
                
                //print tidak boleh kosong
                if(halamanAwal.isEmpty() || halamanAkhir.isEmpty()){
                    JOptionPane.showMessageDialog(parent, "Halaman masih kosong","Warning",JOptionPane.WARNING_MESSAGE);
                }
                //print tidak boleh di luar rentang
                else if((Integer.parseInt(halamanAwal) < 1 || Integer.parseInt(halamanAwal) > jumHal)||
                        (Integer.parseInt(halamanAkhir) < 1 || Integer.parseInt(halamanAkhir) > jumHal)){
                        JOptionPane.showMessageDialog(parent, "Masukkan berada di luar rentang halaman","Error",JOptionPane.ERROR_MESSAGE);
                }
                //Print tidak boleh menurun
                else if(Integer.parseInt(halamanAwal) > Integer.parseInt(halamanAkhir)){
                        JOptionPane.showMessageDialog(parent, "Print tidak boleh menurun","Error",JOptionPane.ERROR_MESSAGE);
                }

                else{
                    confirm = JOptionPane.showOptionDialog(parent, "Apakah anda yakin akan print data:\n"
                                                            +"Kecamatan : "+camat+"\n"
                                                            +"Kelurahan : "+lurah+"\n"
                                                            +"Tahun : "+tahun+"\n"
                                                            +"Halaman : "+halamanAwal+" sampai "+halamanAkhir,"Confirmation", 
                                                            JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, 
                                                            null, null, null);
                    if(confirm == JOptionPane.OK_OPTION){
                        if(Integer.parseInt(halamanAwal) == 1){
                            if(Integer.parseInt(halamanAkhir) == jumHal){
                                parent.PrintAllPage();
                            }
                            else {
                                parent.PrintFromFirstPage();
                            }
                        }
                        else if(Integer.parseInt(halamanAwal) > 1 && Integer.parseInt(halamanAkhir) == jumHal){
                            parent.PrintToLastPage();
                        }
                        else{
                            parent.PrintCustomPage();
                        }          
                    }  
                }
            }
        }
        catch(Exception ex){
            Logger.getLogger(PrintDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_okButtonActionPerformed
    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
     * Closes the dialog
     */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog
   
    private void RBPrintAllMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RBPrintAllMouseClicked
        // TODO add your handling code here:
        
        TFPOP.setEnabled(false);
        TFFrom.setEnabled(false);
        TFTo.setEnabled(false);
        RBPOP.setEnabled(false);
        RBPCP.setEnabled(false);
         
        if(RBPOP.isSelected() == true){
            TFPOP.setText("");
        }
        if(RBPCP.isSelected() == true){
            TFFrom.setText("");
            TFTo.setText("");

        }
        ButtonGroup2.clearSelection();

    }//GEN-LAST:event_RBPrintAllMouseClicked

    private void RBPrintPartMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RBPrintPartMouseClicked
        // TODO add your handling code here:
        RBPOP.setEnabled(true);
        RBPCP.setEnabled(true);
        RBPOP.setSelected(true);
        TFPOP.setEnabled(true);
        TFFrom.setEnabled(false);
        TFTo.setEnabled(false);
        
        
               
    }//GEN-LAST:event_RBPrintPartMouseClicked

    private void RBPOPMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RBPOPMouseClicked
        // TODO add your handling code here:
        if(RBPOP.isSelected() == false){
            TFPOP.setEnabled(false);
        }
        else{
            TFPOP.setEnabled(true);
            TFFrom.setEnabled(false);
            TFTo.setEnabled(false);
            TFFrom.setText("");
            TFTo.setText("");
        }
    }//GEN-LAST:event_RBPOPMouseClicked

    private void RBPCPMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RBPCPMouseClicked
        // TODO add your handling code here:
        if(RBPCP.isSelected() == false){
            TFFrom.setEnabled(false);
            TFTo.setEnabled(false);
        }
        else{
            TFPOP.setEnabled(false);
            TFPOP.setText("");
            TFFrom.setEnabled(true);
            TFTo.setEnabled(true);
        }
    }//GEN-LAST:event_RBPCPMouseClicked

    private void TFPOPKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TFPOPKeyPressed
        // TODO add your handling code here:
        int input = evt.getKeyChar();
        
        int inputBspace  = evt.getKeyCode();
        if((input >= '0' && input <= '9')||(inputBspace == evt.VK_BACK_SPACE)||(inputBspace == evt.VK_DELETE)||(inputBspace == evt.VK_LEFT)||(inputBspace == evt.VK_RIGHT)){
            TFPOP.setEditable(true);
        }
        else{
            TFPOP.setEditable(false);
        }
    }//GEN-LAST:event_TFPOPKeyPressed

    private void TFFromKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TFFromKeyPressed
        // TODO add your handling code here:
         int input = evt.getKeyChar();
        
        int inputBspace  = evt.getKeyCode();
        if((input >= '0' && input <= '9')||(inputBspace == evt.VK_BACK_SPACE)||(inputBspace == evt.VK_DELETE)||(inputBspace == evt.VK_LEFT)||(inputBspace == evt.VK_RIGHT)){
            TFFrom.setEditable(true);
        }
        else{
            TFFrom.setEditable(false);
        }
        
    }//GEN-LAST:event_TFFromKeyPressed

    private void TFToKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TFToKeyPressed
        // TODO add your handling code here:
         int input = evt.getKeyChar();
        
        int inputBspace  = evt.getKeyCode();
        if((input >= '0' && input <= '9')||(inputBspace == evt.VK_BACK_SPACE)||(inputBspace == evt.VK_DELETE)){
            TFTo.setEditable(true);
        }
        else{
            TFTo.setEditable(false);
        }
    }//GEN-LAST:event_TFToKeyPressed
    
    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /*
//         * Set the Nimbus look and feel
//         */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /*
//         * If Nimbus (introduced in Java SE 6) is not available, stay with the
//         * default look and feel. For details see
//         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(PrintDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(PrintDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(PrintDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(PrintDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /*
//         * Create and display the dialog
//         */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                PrintDialog dialog = null;
//                try {
//                    dialog = new PrintDialog(new javax.swing.JFrame(), true);
//                } catch (Exception ex) {
//                    Logger.getLogger(PrintDialog.class.getName()).log(Level.SEVERE, null, ex);
//                }
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup ButtonGroup1;
    private javax.swing.ButtonGroup ButtonGroup2;
    private javax.swing.JRadioButton RBPCP;
    private javax.swing.JRadioButton RBPOP;
    private javax.swing.JRadioButton RBPrintAll;
    private javax.swing.JRadioButton RBPrintPart;
    private javax.swing.JTextField TFFrom;
    private javax.swing.JTextField TFPOP;
    private javax.swing.JTextField TFTo;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
    private int returnStatus = RET_CANCEL;

    public String getOnePage(){
        String onePage = TFPOP.getText();
        return onePage;
    }
    
    public String getPageFrom(){
        String pageFrom = TFFrom.getText();
        return pageFrom;
    }
    
    public String getPageTo(){
        String pageTo = TFTo.getText();
        return pageTo;
    }
    /**
     * @return the ud
     */
}
